package com.czh;


import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

import javax.xml.parsers.SAXParser;
import java.io.File;
import java.util.Collections;

/**
 * Unit test for simple App.
 */
@SpringBootTest
public class CodeGenerator {

    @Value("${spring.datasource.url}")
    String url;
    @Value("${spring.datasource.username}")
    String username;
    @Value("${spring.datasource.password}")
    String password;


    public static final String SEPARATOR = File.separator;
    public static final String SUFFIX =
            "sys-admin" + SEPARATOR + "target" + SEPARATOR + "test-classes" + SEPARATOR + "com" + SEPARATOR + "czh" + SEPARATOR;
    public static final String PATH_TO_SYS_SERVICE = "sys-service" + SEPARATOR + "src" + SEPARATOR + "main" + SEPARATOR;

    static String FINAL_PATH;


    @BeforeAll
    public static void beforeALL() {

        System.out.println(SEPARATOR);
        String path = App.class.getResource("").getPath();
        FINAL_PATH = path.replace(SUFFIX,PATH_TO_SYS_SERVICE);
    }
    @Test
    public  void generate () {

        System.out.println(FINAL_PATH);

        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author("czh") // 设置作者
//                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .commentDate("yyyy-MM-dd")
                            .outputDir(FINAL_PATH + "java"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.czh") // 设置父包名
                            .moduleName("service") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml,FINAL_PATH + "resources" + SEPARATOR + "mapper")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude("students") //TODO 设置需要生成的表名
//                            .addTablePrefix("t_", "c_")
                    .entityBuilder()
                            .enableLombok()
                            .enableTableFieldAnnotation();
//                            .superClass("com.example.BaseEntity");//TODO not 父类
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }


    @Test
    public void t1 () {

    }

}